//
//  LateralSpreadsViewController.m
//  sssssqqq
//
//  Created by 于仁汇 on 16/12/1.
//  Copyright © 2016年 于仁汇. All rights reserved.
//

#import "LateralSpreadsViewController.h"

@interface LateralSpreadsViewController ()

// 主界面的frame
@property (nonatomic, assign) CGRect fullFrame;
// 侧边界面的frame
@property (nonatomic, assign) CGRect leftFrame;
// 是否显示侧边栏
@property (nonatomic, assign) BOOL showLeftView;
// 平移手势，在侧边栏显示的时候，添加在主界面上
@property (nonatomic, strong) UIPanGestureRecognizer *panGestureRecognizer;
// 边缘滑动手势，加载本控制器上面
@property (nonatomic, strong) UIScreenEdgePanGestureRecognizer *screenEdgePanGestureRecognizer;
// 偏移的宽度
@property (nonatomic, assign) int mainDisplayedWidth;

@end

@implementation LateralSpreadsViewController

- (instancetype)init {
    if ([super init]) {
        // 默认偏移 280
        _mainDisplayedWidth = 280;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if (!_screenEdgePanGestureRecognizer) {
        _screenEdgePanGestureRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePanAction:)];
        _screenEdgePanGestureRecognizer.edges = UIRectEdgeLeft;
        [self.view addGestureRecognizer:_screenEdgePanGestureRecognizer];
    }
    if (!_panGestureRecognizer) {
        _panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panActionWhenScreenEdgePan:)];
        [_panGestureRecognizer setEnabled:NO];
    }

    
}

- (void)setMainViewController:(UIViewController *)mainViewController {
    _mainViewController = mainViewController;
    // 记录主界面中view的frame
    _fullFrame = _mainViewController.view.frame;
    [self.view addSubview:mainViewController.view];
}

- (void)setLeftViewController:(UIViewController *)leftViewController {
    _leftViewController = leftViewController;
    [self.view insertSubview:self.leftViewController.view atIndex:0];
    _leftFrame = CGRectMake(0, 0, (_mainDisplayedWidth), _leftViewController.view.frame.size.height);
}

#pragma mark - 手势
- (void)screenEdgePanAction:(UIScreenEdgePanGestureRecognizer *)sender {
//    [self showLeftController:YES];
    CGFloat senderX;
    // 记录手指的位置
    senderX = [sender translationInView:sender.view].x;
    // 手指滑动过的距离小于设置的偏移距离，界面随滑动显示改变的过程
    if (senderX < _mainDisplayedWidth && senderX > 0) {
        self.mainViewController.view.frame = CGRectMake(senderX, _fullFrame.origin.y, _fullFrame.size.width, _fullFrame.size.height);
        self.leftViewController.view.frame = CGRectMake( (-_leftFrame.size.width + senderX), 0, _leftFrame.size.width, _leftFrame.size.height);
    }
    // 当手势结束的时候
    if (sender.state == UIGestureRecognizerStateEnded) {
        // 滑动的距离大于偏移距离，显示侧边栏，否则不显示
        if (senderX > 150) {
            self.mainViewController.view.frame = CGRectMake(_mainDisplayedWidth, _fullFrame.origin.y, _fullFrame.size.width, _fullFrame.size.height);
            self.leftViewController.view.frame = _leftFrame;
            // 侧边栏处于显示状态
            _showLeftView = YES;
            // 开启手势
            [self.mainViewController.view addGestureRecognizer:_panGestureRecognizer];
            [_panGestureRecognizer setEnabled:YES];
        } else {
            self.mainViewController.view.frame = CGRectMake(0, 0, _fullFrame.size.width, _fullFrame.size.height);
        }
    }
}

- (void)panActionWhenScreenEdgePan:(UIPanGestureRecognizer *)sender {
//    [self showRootController:YES];
    CGFloat senderX;
    senderX = [sender translationInView:sender.view].x;
    // 界面随滑动显示改变的过程
    if (senderX < 0 && senderX > (-_mainDisplayedWidth)) {
        self.mainViewController.view.frame = CGRectMake(_mainDisplayedWidth + senderX, _fullFrame.origin.y, _fullFrame.size.width, _fullFrame.size.height);
        self.leftViewController.view.frame = CGRectMake(senderX, 0, _leftFrame.size.width, _leftFrame.size.height);
    }
    // 手势结束
    if (sender.state == UIGestureRecognizerStateEnded) {
        if (senderX < -100) {
            self.mainViewController.view.frame = _fullFrame;
            [_panGestureRecognizer setEnabled:NO];
            // 侧边栏处于显示状态
            _showLeftView = NO;
        } else {
            self.mainViewController.view.frame = CGRectMake(_mainDisplayedWidth, _fullFrame.origin.y, _fullFrame.size.width, _fullFrame.size.height);
            self.leftViewController.view.frame = _leftFrame;
        }
    }
}

#pragma mark - 显示左侧视图
- (void)showLeftController:(BOOL)animated {
    // 取到左边菜单栏控制器
    UIView *view = self.leftViewController.view;
    CGRect frame = _leftFrame;
    view.frame = frame;// 设置即将显示的菜单栏的frame
    [self.view insertSubview:view atIndex:0];
    [self.leftViewController viewWillAppear:animated];
    // 设置主视图偏移
    CGRect mainViewframe = self.fullFrame;
    // 主视图控制器的frame的x设置为菜单的右x - 菜单栏不显示的宽度
    mainViewframe.origin.x = _mainDisplayedWidth;
    if (!animated) {
        [UIView setAnimationsEnabled:NO];
    }
    [UIView animateWithDuration:.3 animations:^{
        // 设置frame
        self.mainViewController.view.frame = mainViewframe;
        // 设置中心点
        self.mainViewController.view.center = [self pointOfFrame:mainViewframe];
        // 设置缩放比例
        self.mainViewController.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 1);
    } completion:^(BOOL finished) {
        _showLeftView = YES;
        // 激活单击手势
        [self.mainViewController.view addGestureRecognizer:_panGestureRecognizer];
        [_panGestureRecognizer setEnabled:YES];
    }];
}

// 设置中心点
- (CGPoint)pointOfFrame:(CGRect)frame{
    return CGPointMake(frame.origin.x + frame.size.width / 2, self.view.frame.size.height / 2.0);
}

#pragma mark - 隐藏左边的视图
- (void)showRootController:(BOOL)animated {
    // 让单击手势不能响应
    //    [_tap setEnabled:NO];
    // 设置跟视图能够响应
    self.mainViewController.view.userInteractionEnabled = YES;
    [UIView animateWithDuration:.3 animations:^{
        self.mainViewController.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 1);
        self.mainViewController.view.frame = CGRectMake(0, 0, _fullFrame.size.width, _fullFrame.size.height);
    } completion:^(BOOL finished) {
        _showLeftView = NO;
        if (_panGestureRecognizer) {
            [_panGestureRecognizer setEnabled:NO];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
